/**
 * @file name TBPerson.java
 */
package com.digital.community;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.digital.community.common.utils.HttpUtils;
import com.digital.community.common.utils.StringUtils;
import com.digital.community.common.utils.ToastUtils;
import com.digital.community.common.utils.UnitsUtils;

/**
 * 
 * @author yarine.gao
 * @date 2012-11-3
 * @version 1.0.0
 */
public class TBPersonActivity extends Activity {
    
	private ViewPager myViewPager;
	private MyPagerAdapter myAdapter;
	private List<View> mListViews;
	
	private int preIndex = 0;
	
	private View searchLayout = null;
	private View addLayout = null;
	private View mapLayout = null;
	private View listLayout = null;
	
	private LayoutInflater mInflater;
	
	private ImageView tranImg;
	private int tranImgWidth = 0;
	
	private boolean isLoadPager3 = false;
	
	private ArrayList<String> nationList;
	private ArrayList<String> cardtypeList;
	private ArrayList<String> genderList;
	
	private ImageView[] cateIcon = null;
	private TextView[] cateTxt = null;
	Integer[] imgArrSelected = new Integer[4];
	Integer[] imgArrCommon = new Integer[4];
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tbperson);
        
        Intent intent = getIntent();
        nationList = intent.getStringArrayListExtra("nation");
        cardtypeList = intent.getStringArrayListExtra("cardtype");
        genderList = intent.getStringArrayListExtra("gender");
        
        mInflater = getLayoutInflater();
        
        myAdapter = new MyPagerAdapter();
        searchLayout = mInflater.inflate(R.layout.tbperson_search, null);
        addLayout = mInflater.inflate(R.layout.tbperson_add, null);
        mapLayout = mInflater.inflate(R.layout.tbperson_map, null);
        listLayout = mInflater.inflate(R.layout.tbperson_list, null);
        
        initDisplayInfo();
        
        mListViews = new ArrayList<View>();
		mListViews.add(searchLayout);
		mListViews.add(addLayout);
		mListViews.add(mapLayout);
		mListViews.add(listLayout);
		
		cateIcon = new ImageView[4];
		cateTxt = new TextView[4];
		cateIcon[0] = (ImageView)findViewById(R.id.footer_dept_btn);
		cateTxt[0] = (TextView)findViewById(R.id.dept_dept_text);
		
		cateIcon[1] = (ImageView)findViewById(R.id.footer_name_btn);
		cateTxt[1] = (TextView)findViewById(R.id.dept_name_text);
		
		cateIcon[2] = (ImageView)findViewById(R.id.footer_num_btn);
		cateTxt[2] = (TextView)findViewById(R.id.dept_num_text);
		
		cateIcon[3] = (ImageView)findViewById(R.id.footer_num_btn2);
		cateTxt[3] = (TextView)findViewById(R.id.dept_num_text2);
		
		imgArrSelected[0] = R.drawable.a_on;
		imgArrCommon[0] = R.drawable.a_off;
		
		imgArrSelected[1] = R.drawable.b_on;
		imgArrCommon[1] = R.drawable.b_off;
		
		imgArrSelected[2] = R.drawable.c_on;
		imgArrCommon[2] = R.drawable.c_off;
		
		imgArrSelected[3] = R.drawable.d_on;
		imgArrCommon[3] = R.drawable.d_off;
		
		
        myViewPager = (ViewPager)findViewById(R.id.view_pager);
        myViewPager.setAdapter(myAdapter);
        myViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageSelected(int arg0) {
				toTran(arg0);
				
//				if(arg0 == PAGER_GEN && !isInitGeneralContacts) {
//					toInitGeneralContacts();
//				}
				if(preIndex != arg0) {
					cateIcon[arg0].setImageResource(imgArrSelected[arg0]);
					cateTxt[arg0].setTextColor(getResources().getColor(R.color.tb_person_category));
					
					cateIcon[preIndex].setImageResource(imgArrCommon[preIndex]);
					cateTxt[preIndex].setTextColor(getResources().getColor(R.color.tb_person_category2));
					
					preIndex = arg0; 
				}
				
				
				if(!isLoadPager3 && arg0 == 3) {
					toQueryListDate();
				}
			}
        });
        
        
        tranImg = (ImageView)findViewById(R.id.tranImg);
        tranImgWidth = (displayWidth - 2 * (UnitsUtils.dipToPx(20, scale))) / 4;
        tranImg.getLayoutParams().width = tranImgWidth;
        
        initQueryPerson();
        initAddPerson();
        initListPerson();
    }
    
    @Override
    protected void onStart() {
    	super.onStart();
    	

    	if(isLoadPager3) {
            isLoading = false;
            currPage = 1;
        	totalRows = 0;
        	position = 0;
        	 list = new ArrayList<Bundle>();
    		toQueryListDate();
    	}
    }
    
    private EditText searchNameET;
    private EditText searchMobileET;
    private EditText searchWorkET;
    private EditText searchCardnoET;
    private void initQueryPerson() {
    	searchNameET = (EditText)searchLayout.findViewById(R.id.et_name);
    	searchMobileET = (EditText)searchLayout.findViewById(R.id.et_mobile);
    	searchWorkET = (EditText)searchLayout.findViewById(R.id.et_work);
    	searchCardnoET = (EditText)searchLayout.findViewById(R.id.et_cardno);
    }
    
    public void toQueryPerson(View view) {
    	Intent intent = new Intent(this, PListActivity.class);
    	intent.putExtra("name", searchNameET.getText().toString());
    	intent.putExtra("mobile", searchMobileET.getText().toString());
    	intent.putExtra("work", searchWorkET.getText().toString());
    	intent.putExtra("cardno", searchCardnoET.getText().toString());
		intent.putStringArrayListExtra("nation", nationList);
		intent.putStringArrayListExtra("cardtype", cardtypeList);
		intent.putStringArrayListExtra("gender", genderList);
    	startActivity(intent);
    }
    
    private EditText nameET;
    private Spinner genderSpinner;
    private Spinner nationSpinner;
    private Spinner cardTypeSpinner;
    private EditText cardNoET;
    private void initAddPerson() {
    	nameET = (EditText)addLayout.findViewById(R.id.et_name);
    	genderSpinner = (Spinner)addLayout.findViewById(R.id.sp_gender);
    	nationSpinner = (Spinner)addLayout.findViewById(R.id.sp_nation);
    	cardTypeSpinner  = (Spinner)addLayout.findViewById(R.id.sp_cardtype);
    	cardNoET  = (EditText)addLayout.findViewById(R.id.et_cardno);
    	
    	ArrayAdapter genderSpinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, genderList);
    	genderSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	genderSpinner.setAdapter(genderSpinnerAdapter); 
    	
    	ArrayAdapter nationSpinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, nationList);
    	nationSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	nationSpinner.setAdapter(nationSpinnerAdapter);
    	
    	ArrayAdapter cardTypeSpinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cardtypeList);
    	cardTypeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	cardTypeSpinner.setAdapter(cardTypeSpinnerAdapter);
    }
    
    private boolean isLoading = false;
    private int currPage = 1;
	private int totalRows = 0;
	private int position = 0;
    private ListView plist;
    
    private TextView nameTxt;
    private TextView genderTxt;
    private TextView nationTxt;
    private TextView updateTxt;
    private TextView delTxt;
    private void initListPerson() {
    	
    	nameTxt = (TextView)listLayout.findViewById(R.id.txt_name);
        genderTxt = (TextView)listLayout.findViewById(R.id.txt_gender);
        nationTxt = (TextView)listLayout.findViewById(R.id.txt_nation);
        updateTxt = (TextView)listLayout.findViewById(R.id.txt_update);
        delTxt = (TextView)listLayout.findViewById(R.id.txt_del);
    	
    	list = new ArrayList<Bundle>();
    	plist = (ListView)listLayout.findViewById(R.id.list);
    	plist.setOnScrollListener(new AbsListView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if (view.getAdapter() != null && view.getCount() > 0) {
					System.out.println("---->firstVisibleItem: " + firstVisibleItem + "  visibleItemCount: " + visibleItemCount
					+ "  totalItemCount :" + totalItemCount + "  totalRows: " + totalRows);
					if (firstVisibleItem + visibleItemCount == totalItemCount
							&& totalItemCount < totalRows && !isLoading) {
						currPage++;
						position = totalItemCount;
						
						toQueryListDate();
					}
				}
			}
		});
    }
    
    
    private String name;
    private String gender;
    private String nation;
    private String cardType;
    private String cardNo;
    public void toAddPerson(View view) {
    	name = nameET.getText().toString();
    	gender = genderSpinner.getSelectedItem().toString();
    	nation = nationSpinner.getSelectedItem().toString();
    	cardType = cardTypeSpinner.getSelectedItem().toString();
    	cardNo = cardNoET.getText().toString();
    	
    	if(StringUtils.isBlank(name)) {
    		ToastUtils.showErrorToast(this, getLayoutInflater(), "姓名不能为空!", false);
    		return;
    	}
    	if(StringUtils.isBlank(gender)) {
    		ToastUtils.showErrorToast(this, getLayoutInflater(), "性别不能为空!", false);
    		return;
    	}
    	if(StringUtils.isBlank(nation)) {
    		ToastUtils.showErrorToast(this, getLayoutInflater(), "民族不能为空!", false);
    		return;
    	}
    	if(StringUtils.isBlank(cardNo)) {
    		ToastUtils.showErrorToast(this, getLayoutInflater(), "证件号不能为空!", false);
    		return;
    	}
    	
    	
    	toAddPerson();
    }
    
    ProgressDialog proDialog;
    private void toAddPerson() {
    	proDialog = ProgressDialog.show(TBPersonActivity.this, "请等待", "正在请求数据...",true,true);
    	new Thread() {
    		public void run() {
    			HashMap<String, String> params = new HashMap<String, String>();
    	    	params.put("name", name);
    	    	params.put("gender", gender);
    	    	params.put("nation", nation);
    	    	params.put("cardType", cardType);
    	    	params.put("cardNo", cardNo);
    	    	
    	    	Bundle bundle = HttpUtils.getPost("toAdd.do", params, 3000, TBPersonActivity.this);
    	    	if(bundle.getInt("code") == HttpUtils.SUCCESS) {
    	    		String content = bundle.getString("content");
    	    		if(StringUtils.isBlank(content) || "[]".equals(content)) {
    	    			handler.sendEmptyMessage(HANDLER_CONN_ERROR);
    	    			return;
    	    		} else {
    	    			try {
    	    				JSONObject json = new JSONObject(content);
    	    				int success = json.getInt("success");
    	    				if(success == 1) {
    	    					handler.sendEmptyMessage(HANDLER_ADD_SUCCESS);
    	    					return;
    	    				} else {
    	    					handler.sendEmptyMessage(HANDLER_ADD_ERROR);
    	    					return;
    	    				}
    	    			} catch (Exception e) {
    	    				System.out.println(e);
    	    				handler.sendEmptyMessage(HANDLER_CONN_ERROR);
						}
    	    		}
    	    	} else {
    	    		handler.sendEmptyMessage(HANDLER_CONN_ERROR);
    	    	}
    		};
    	}.start();
    }
    
    private ArrayList<Bundle> list = null;
    private void toQueryListDate() {
    	if(isLoading) {
			return;
		}
    	isLoading = true;
    	proDialog = ProgressDialog.show(TBPersonActivity.this, "请等待", "正在请求数据...",true,true);
    	new Thread() {
    		public void run() {
    			HashMap<String, String> params = new HashMap<String, String>();
    	    	
    	    	Bundle bundle = HttpUtils.getPost("toQuery.do", params, 3000, TBPersonActivity.this);
    	    	if(bundle.getInt("code") == HttpUtils.SUCCESS) {
    	    		String content = bundle.getString("content");
    	    		if(StringUtils.isBlank(content) || "{}".equals(content)) {
    	    			handler.sendEmptyMessage(HANDLER_QUERY_CONN_ERROR);
    	    			return;
    	    		} else {
    	    			try {
    	    				JSONArray json = new JSONArray(content);
    	    				totalRows = json.getInt(0);
    	    				for(int i = 1; i < json.length(); i ++) {
    	    					JSONObject obj = json.getJSONObject(i);
    	    					Bundle temp = new Bundle();
    	    					temp.putString("name", obj.getString("name"));
    	    					temp.putString("gender", obj.getString("gender"));
    	    					temp.putString("nation", obj.getString("nation"));
    	    					temp.putString("cardno", obj.getString("cardno"));
    	    					temp.putString("guid", obj.getString("guid"));
    	    					
    	    					list.add(temp);
    	    				}
    	    				
    	    				handler.sendEmptyMessage(HANDLER_CQUERY_CONN_SUCCESS);
    	    			} catch (Exception e) {
    	    				System.out.println(e);
    	    				handler.sendEmptyMessage(HANDLER_QUERY_CONN_ERROR);
						}
    	    		}
    	    	} else {
    	    		handler.sendEmptyMessage(HANDLER_QUERY_CONN_ERROR);
    	    	}
    		};
    	}.start();
    }
    
    private class PersonQueryAdapter extends BaseAdapter {

    	private ArrayList<Bundle> list;
    	private LayoutInflater inflater;
    	
    	public PersonQueryAdapter(ArrayList<Bundle> list, Context c) {
    		this.list = list;
    		inflater = LayoutInflater.from(c);
    	}
    	
		@Override
		public int getCount() {
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int arg0) {
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			ViewHolder holder;
			if(convertView == null) {
				convertView = (LinearLayout)inflater.inflate(R.layout.tbperson_list_item, null);
				holder = new ViewHolder();
				holder.nameTxt = (TextView)convertView.findViewById(R.id.txt_name);
				holder.genderTxt = (TextView)convertView.findViewById(R.id.txt_gender);
				holder.nationTxt = (TextView)convertView.findViewById(R.id.txt_nation);
				holder.updateTxt = (TextView)convertView.findViewById(R.id.txt_update);
				holder.delTxt = (TextView)convertView.findViewById(R.id.txt_del);
				holder.nameLyout = (LinearLayout)convertView.findViewById(R.id.lyout_name);
				
				holder.nameLyout.getLayoutParams().width = nameWidth;
				holder.genderTxt.setWidth(genderWidth);
				holder.nationTxt.setWidth(nationWidth);
				holder.updateTxt.setWidth(updateWidth);
				holder.delTxt.setWidth(delWidth);
				
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder)convertView.getTag();
			}
			
			Bundle bundle = list.get(arg0);
			holder.nameTxt.setText(bundle.getString("name"));
			holder.genderTxt.setText(bundle.getString("gender"));
			holder.nationTxt.setText(bundle.getString("nation"));
			holder.updateTxt.setTag(bundle.getString("guid"));
			holder.delTxt.setTag(bundle.getString("guid"));
			
			holder.updateTxt.setOnClickListener(new UpdateClickListener());
			holder.delTxt.setOnClickListener(new DelClickListener());
			
			return convertView;
		}
    }
    
    class ViewHolder {
    	LinearLayout nameLyout;
    	TextView nameTxt;
    	TextView genderTxt;
    	TextView nationTxt;
    	TextView updateTxt;
    	TextView delTxt;
    }
    
    private int nameWidth = 0;
	private int genderWidth = 0;
	private int nationWidth = 0;
	private int updateWidth = 0;
	private int delWidth = 0;
	
    private static final int HANDLER_CONN_ERROR = 100;
    private static final int HANDLER_ADD_SUCCESS = 101;
    private static final int HANDLER_ADD_ERROR = 102;
    private static final int HANDLER_QUERY_CONN_ERROR = 103;
    private static final int HANDLER_CQUERY_CONN_SUCCESS = 104;
    private static final int HANDLER_DEL_CONN_ERROR = 105;
    private static final int HANDLER_DEL_ERROR = 106;
    private static final int HANDLER_DEL_SUCCESS = 107;
    Handler handler = new Handler() {
    	
    	public void handleMessage(android.os.Message msg) {
    		if(proDialog != null && proDialog.isShowing()) {
				proDialog.dismiss();
			}
    		
    		switch (msg.what) {
			case HANDLER_CONN_ERROR:
				showDialog(DIALOG_NETWORK_ERROR);
				break;
			case HANDLER_ADD_SUCCESS:
				ToastUtils.showTrueToast(TBPersonActivity.this, getLayoutInflater(), "添加成功!", false);
				break;
			case HANDLER_ADD_ERROR:
				showDialog(DIALOG_ADD_ERROR);
				break;
			case HANDLER_QUERY_CONN_ERROR:
				isLoadPager3 = true;
				isLoading = false;
				showDialog(DIALOG_QUERY_NETWORK_ERROR);
				break;
			case HANDLER_CQUERY_CONN_SUCCESS:
				
				nameWidth = nameTxt.getWidth();
				genderWidth = genderTxt.getWidth();
				nationWidth = nationTxt.getWidth();
				updateWidth = updateTxt.getWidth();
				delWidth = delTxt.getWidth();
				
				isLoading = false;
				isLoadPager3 = true;
				if (list != null && list.size() > 0) {
					plist.setAdapter(new PersonQueryAdapter(list, TBPersonActivity.this));
					if (position > 0) {
						plist.setSelection(position);
					}
				} else {
					plist.setAdapter(null);
				}
				break;
			case HANDLER_DEL_CONN_ERROR:
				showDialog(DIALOG_DEL_CONN_ERROR);
				break;
			case HANDLER_DEL_ERROR:
				showDialog(DIALOG_DEL_ERROR);
				break;
			case HANDLER_DEL_SUCCESS:
				ToastUtils.showTrueToast(TBPersonActivity.this, getLayoutInflater(), "注销成功!", false);
				
				isLoading = false;
				currPage = 1;
				totalRows = 0;
				position = 0;
				list = new ArrayList<Bundle>();
				toQueryListDate();
				break;
			default:
				break;
			}
    		
    	};
    };
    
    
    private static final int DIALOG_NETWORK_ERROR = 200;
    private static final int DIALOG_ADD_ERROR = 201;
    private static final int DIALOG_QUERY_NETWORK_ERROR = 202;
    private static final int DIALOG_DEL_CONN_ERROR = 203;
    private static final int DIALOG_DEL_ERROR = 204;
    @Override
	protected Dialog onCreateDialog(int id) {
		if(!isActive) return null;
		switch (id) {
		case DIALOG_NETWORK_ERROR:
			return new AlertDialog.Builder(this).setTitle("提示信息").setMessage("对不起,网络联接失败!")
			.setPositiveButton("重试", new RetryAdd())
			.setNegativeButton("取消", new Cancle()).create();
		case DIALOG_ADD_ERROR:
			return new AlertDialog.Builder(this).setTitle("提示信息").setMessage("对不起,添加失败!")
						.setPositiveButton("重试", new RetryAdd())
						.setNegativeButton("取消", new Cancle()).create();
		case DIALOG_QUERY_NETWORK_ERROR:
			return new AlertDialog.Builder(this).setTitle("提示信息").setMessage("对不起,网络联接失败!")
						.setPositiveButton("重试", new RetryQuery())
						.setNegativeButton("取消", new Cancle()).create();
		case DIALOG_DEL_CONN_ERROR:
			return new AlertDialog.Builder(this).setTitle("提示信息").setMessage("对不起,网络联接失败!")
			.setPositiveButton("重试", new RetryDel())
			.setNegativeButton("取消", new Cancle()).create();
		case DIALOG_DEL_ERROR:
			return new AlertDialog.Builder(this).setTitle("提示信息").setMessage("对不起,注销失败!")
						.setPositiveButton("重试", new RetryDel())
						.setNegativeButton("取消", new Cancle()).create();
		default:
			return null;
		}
	}
    
    private class RetryAdd implements OnClickListener {
    	@Override
    	public void onClick(DialogInterface dialog, int which) {
    		toAddPerson();
    	}
    }
    private class RetryQuery implements OnClickListener {
    	@Override
    	public void onClick(DialogInterface dialog, int which) {
    		toQueryListDate();
    	}
    }
    private class RetryDel implements OnClickListener {
    	@Override
    	public void onClick(DialogInterface dialog, int which) {
    		toDelete();
    	}
    }
    
    private class Cancle implements OnClickListener {
		@Override
		public void onClick(DialogInterface dialog, int which) {
		}
    }
    
    private boolean isActive = true;
    @Override
    protected void onDestroy() {
    	isActive = false;
    	super.onDestroy();
    }
    
    
    
    
    
    private class MyPagerAdapter extends PagerAdapter{
		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager)arg0).removeView(mListViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mListViews.size();
		}

		@Override
		public Object instantiateItem(View view, int pos) {
			((ViewPager)view).addView(mListViews.get(pos),0);
			return mListViews.get(pos);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0==(arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
		
		@Override
		public void notifyDataSetChanged() {
			super.notifyDataSetChanged();
		}
    }
    
    int pagerPos = 0;
    float padding = 37.5f;
    public void toTran(int newPos) {
    	translateAnimation(newPos);
    	
        pagerPos = newPos;
    }
    
    private void translateAnimation(int newPos) {
    	
    	TranslateAnimation tran = new TranslateAnimation((tranImgWidth * pagerPos), (tranImgWidth * newPos), 0, 0);
    	tran.setDuration(400);
    	tran.setFillAfter(true);
    	tranImg.setAnimation(tran);
    }
    
    private int displayWidth;
    private float scale;
    private void initDisplayInfo() {
    	Display display = getWindowManager().getDefaultDisplay();
        displayWidth = display.getWidth();
        
        scale = getResources().getDisplayMetrics().density;
    }
    
    public void toSearchPage(View view) {
    	myViewPager.setCurrentItem(0);
    }
    
    public void toAddPage(View view) {
    	myViewPager.setCurrentItem(1);
    }
    
    public void toMapPage(View view) {
    	myViewPager.setCurrentItem(2);
    }
    
    public void toListPage(View view) {
    	myViewPager.setCurrentItem(3);
    }
    
    private class DelClickListener implements android.view.View.OnClickListener {
    	@Override
    	public void onClick(View v) {
    		guid = (String)v.getTag();
    		toDelete();
    	}
    }
    private class UpdateClickListener implements android.view.View.OnClickListener {
		@Override
		public void onClick(View v) {
			guid = (String)v.getTag();
			Intent intent = new Intent(TBPersonActivity.this, TBPersonEditActivity.class);
			intent.putExtra("guid", guid);
			intent.putStringArrayListExtra("nation", nationList);
			intent.putStringArrayListExtra("cardtype", cardtypeList);
			intent.putStringArrayListExtra("gender", genderList);
			TBPersonActivity.this.startActivity(intent);
		}
    }
    
    private String guid;
    private void toDelete() {
    	proDialog = ProgressDialog.show(TBPersonActivity.this, "请等待", "正在请求数据...",true,true);
    	new Thread() {
    		public void run() {
    			HashMap<String, String> params = new HashMap<String, String>();
    			params.put("guid", guid);
    			Bundle bundle = HttpUtils.getPost("delPerson.do", params, 3000, TBPersonActivity.this);
    			
    			if(bundle.getInt("code") == HttpUtils.SUCCESS) {
    	    		String content = bundle.getString("content");
    	    		if(StringUtils.isBlank(content) || "{}".equals(content)) {
    	    			handler.sendEmptyMessage(HANDLER_DEL_CONN_ERROR);
    	    			return;
    	    		} else {
    	    			try {
    	    				JSONObject json = new JSONObject(content);
    	    				int success = json.getInt("success");
    	    				if(success == 1) {
    	    					handler.sendEmptyMessage(HANDLER_DEL_SUCCESS);
    	    				} else {
    	    					handler.sendEmptyMessage(HANDLER_DEL_ERROR);
    	    				}
    	    			} catch (Exception e) {
    	    				System.out.println(e);
    	    				handler.sendEmptyMessage(HANDLER_DEL_CONN_ERROR);
						}
    	    		}
    	    	} else {
    	    		handler.sendEmptyMessage(HANDLER_DEL_CONN_ERROR);
    	    	}
    		};
    	}.start();
    }
}
